package com.kc.web.controller.business;

import com.kc.business.domain.BizRole;
import com.kc.business.service.IBizRoleService;
import com.kc.common.annotation.Log;
import com.kc.common.annotation.RepeatSubmit;
import com.kc.common.core.controller.BaseController;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.core.domain.model.LoginUser;
import com.kc.common.core.page.TableDataInfo;
import com.kc.common.enums.BusinessType;
import com.kc.common.utils.ServletUtils;
import com.kc.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * FileName: BizRoleController
 * Author:   TP
 * Date:     2020-10-30 19:31
 * Description: 角色控制器
 */
@RestController
@RequestMapping("/business/role")
public class BizRoleController extends BaseController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IBizRoleService bizRoleService;

    /**
     * 角色列表
     */
    @PreAuthorize("@ss.hasPermi('business:role:list')")
    @GetMapping("/list")
    public TableDataInfo getList(BizRole role) {
        startPage();
        List<BizRole> list = bizRoleService.getList(role);
        return getDataTable(list);
    }

    /**
     * 新增
     */
    @RepeatSubmit
    @Log(title = "新增角色", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('business:role:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody @Validated BizRole role) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (null == loginUser) {
            return AjaxResult.error("操作失败，未获取到当前登陆人信息");
        }
        role.setCreateBy(loginUser.getUsername());
        role.setCreateTime(new Date());
        return bizRoleService.add(role, loginUser.getUsername());
    }

    /**
     * 修改
     */
    @Log(title = "修改角色", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('business:role:update')")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody @Validated BizRole role) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (null == loginUser) {
            return AjaxResult.error("操作失败，未获取到当前登陆人信息");
        }
        return bizRoleService.update(role, loginUser.getUsername());
    }

    /**
     * 删除
     */
    @Log(title = "删除角色", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('business:role:delete')")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (null == loginUser) {
            return AjaxResult.error("操作失败，未获取到当前登陆人信息");
        }
        return bizRoleService.deleteById(id, loginUser.getUsername());
    }
}
